var ResetForm = Class.create();
Object.extend(ResetForm, {
	initMailHandlers:function(){
		$('checknicknameBtn').observe('click',this.verifyMail.bindAsEventListener(this));
	},
	verifyMail:function(event){
		event = $E(event);
		event.stop();
		var email = $('email').value.trim();
		if(email.length<= 0){
			alert('请您输入邮箱地址！');
			$('email').focus();
			return false;
		}
		if(!this.checkMail(email)){
			alert('请您输入正确的邮箱地址！');
			$('email').focus();
			return false;
		}
		$('form1').request({
			onComplete:function(transport){
				var ret = transport.responseText.evalJSON();
				try{
					if(ret.result){
						$('reset_pwd').innerHTML = '<div class="getback-middletwo"><h2>一封确认信已经发送到您的注册邮箱 &nbsp;&nbsp;'+ret.data+'中&nbsp;&nbsp;</h2><div class="getback-middletwoborder"><div class="getback-middletwomr"><p>动漫国际的网友您好：邮件已经发送到您的邮箱，使用其中的地址您可以修改密码，如果找不到邮件请检查垃圾邮件。</p><p>如果你没有收到新密码的电子邮件，请联系客服。（客服邮箱：　客服电话：）</p></div><!--getback-middletwomr--></div><!--getback-middletwoborder--></div>';
					}else{
						$('reset_pwd').innerHTML = '<div class="getback-middletwo"><h2>发送邮件错误，请稍候再试！</h2></div>'
					}
				}catch(e){
					
				}
			}
		});
	},
	init2Handlers: function(){
		$("resetForm").observe("submit", this.verifyAnswer.bindAsEventListener(this));
	},
	verifyAnswer: function(event){
		event = $E(event);
		event.stop();
		var form = this;
		if($("answer").value.trim() == "" ) return;
		
		var template = new Template(RegisterForm.promptTemplate);
		$("resetForm").request({
			onComplete: function(transport){
				var result = transport.responseText;
				try{
					var ret = result.evalJSON();
					if(ret.result){
						window.location.href = "/?" + 
							$H({
								p:	 "reset_pass3", 
								k:	 ret.data.k,
								uid: ret.data.uid 
							}).toQueryString();
					}else{
						ret.reason.each(function(v){
							var field = $$("#"+v.src+"Field .check")[0];
							field.innerHTML = template.evaluate({string: v.msg});
						});
					}
				}catch(e){
//					alert(e);
				}
			}
		});
	},
	init3Handlers: function(){
		$("resetForm").observe("submit", this.resetPassword.bindAsEventListener(this));
	},
	resetPassword: function(event){
		event = $E(event);
		event.stop();
		var form = this;
		var template = new Template(RegisterForm.promptTemplate);
		
		if(!this.checkPassword()){
			$$("#password2Field .check")[0].innerHTML = template.evaluate({string: "密码长度应为6-15位，请确保两次输入的密码一致"});
			return false;
		}
		$("resetForm").request({
			onComplete: function(transport){
				var result = transport.responseText;
				try{
					var ret = result.evalJSON();
					if(ret.result){
						var email = ret.data.email;
						window.location.href = "http://i.manhuaguoji.com/~member.login.login";
					}else{
						ret.reason.each(function(v){
							var field = $$("#"+v.src+"Field .check")[0];
							field.innerHTML = template.evaluate({string: v.msg});
						});
					}
				}catch(e){	}
			}
		});
	},
	checkPassword: function (){
		return ($("resetForm").password.value.length>=6 && $("resetForm").password.value.length<=15) && $("resetForm").password.value == $("resetForm").password2.value; 
	},
	reloadVcode: function(){
		$( "vcodeimg").src= "/q_vcode.php?_act=gen_rp&"+Math.random();
		$( "vcode").value = "";
	},
	checkMail:function(str){
		return /^\w+(?:[-+.]\w+)*@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*$/.test(str);
	}
});

var RenewForm = Class.create();

Object.extend(RenewForm, {
	promptTemplate: '<span class="prompt prompt-span"><span class="icon-prompt">&nbsp;</span>#{string}</span>',
	initHandlers: function(){
		$("renewForm").observe("submit", this.submitHandler.bindAsEventListener(this));
	},
	submitHandler: function(event){
		event = $E(event);
		event.stop();
		var form = this;
		var template = new Template(this.promptTemplate);
		
		if(!this.checkForm()){
			return;
		}

		$("renewForm").request({
			onComplete: function(transport){
				var result = transport.responseText;
				try{
					var ret = result.evalJSON();
					if(ret.result){
						//alert(1);
						window.location.href = "/?" + 
							$H({
								p:"renew_pass_success", 
								next: "/?"+$H({p: "home"}).toQueryString()
							}).toQueryString();
					}else{
						ret.reason.each(function(v){
							var field = $$("#"+v.src+"Field .check")[0];
							field.innerHTML = template.evaluate({string: v.msg});
						});
					}
				}catch(e){
	
				}
			}
		});
	},
	checkForm: function(){
		var ret = true;
		var flag = true;
		var template = new Template(this.promptTemplate);
		var field = $$("#oldpasswordField .check")[0];
		if($("renewForm").old_pass.value.trim()=="") {
			flag = false;
			field.innerHTML = template.evaluate({string: "原密码不能为空"});
		}else{
			flag = true;
			field.innerHTML = '';
		}
		ret = ret && flag;

		var field = $$("#passwordField .check")[0];
		if($("renewForm").new_pass.value.trim().length<6 ||  $("new_pass").value.trim().length>15) {
			flag = false;
			field.innerHTML = template.evaluate({string: "密码格式错误"});
		}else{
			flag = true;
			field.innerHTML = '';

			field = $$("#password2Field .check")[0];
			if($("renewForm").new_pass.value.trim() != $("renewForm").new_pass2.value.trim()){
				flag = false;
				field.innerHTML = template.evaluate({string: '密码不一致'});
			}else{
				flag = true;
				field.innerHTML = '';
			}
			ret = ret && flag;
		}
		ret = ret && flag;

		return ret;
//		return ($("new_pass").value.length>=6 && $("new_pass").value.length<=15) && ($("new_pass").value == $("new_pass2").value);
	}
});